package com.nkl.page.action;

import com.nkl.common.action.BaseAction;
import com.nkl.common.util.Param;
import com.nkl.page.domain.Comment;
import com.nkl.page.domain.CommentReply;
import com.nkl.page.domain.Infos;
import com.nkl.page.domain.Note;
import com.nkl.page.manager.IndexManager;
import org.apache.struts2.json.annotations.JSON;

import java.util.Arrays;
import java.util.List;

//import javax.servlet.ServletContext;
//import org.apache.struts2.ServletActionContext;
//import com.nkl.common.util.FindProjectPath;

//import com.opensymphony.xwork2.ActionContext;

public class IndexAction extends BaseAction {

	private static final long serialVersionUID = 1L;
	IndexManager indexManager = new IndexManager();

	// 抓取页面参数
	Infos paramsInfos;
	Comment paramsComment;
	CommentReply paramsCommentReply;
	Note paramsNote;
	String tip;

	public String index() {
		try {
			// 查询最新资讯前9条
			Infos infos = new Infos();
			infos.setStart(0);
			infos.setLimit(9);
			infos.setInfo_type(1);
			List<Infos> infoss1 = indexManager.listInfoss(infos, null);

			// 查询活动公告
			Note note = new Note();
			note.setStart(0);
			note.setLimit(10);
			List<Note> notes = indexManager.listNotes(note, null);

			// 查询学术动态
			infos.setStart(0);
			infos.setLimit(5);
			infos.setInfo_type(2);
			List<Infos> infoss2 = indexManager.listInfoss(infos, null);

			// 查询奖励资助
			infos.setInfo_type(3);
			List<Infos> infoss3 = indexManager.listInfoss(infos, null);

			// 查询老生常谈
			infos.setInfo_type(4);
			List<Infos> infoss4 = indexManager.listInfoss(infos, null);

			// 查询新闻评论
			Comment comment = new Comment();
			comment.setStart(0);
			comment.setLimit(5);
			List<Comment> comments = indexManager.listComments(comment, null);

			Param.setAttribute("notes", notes);
			Param.setAttribute("infoss1", infoss1);
			Param.setAttribute("infoss2", infoss2);
			Param.setAttribute("infoss3", infoss3);
			Param.setAttribute("infoss4", infoss4);
			Param.setAttribute("comments", comments);

		} catch (Exception e) {
			e.printStackTrace();
			return "error";
		}

		return "index";
	}

	/**
	 * @Title: listInfoss
	 * @Description: 查询 最新资讯 学术动态 奖励资助
	 * @return String
	 */
	public String listInfoss() {
		try {
			// 查询 最新资讯 学术动态 奖励资助
			setPagination(paramsInfos);
			int[] sum = { 0 };
			List<Infos> infoss = indexManager.listInfoss(paramsInfos, sum);

			Param.setAttribute("infoss", infoss);
			setTotalCount(sum[0]);
			Param.setAttribute("info_type", paramsInfos.getInfo_type());
			Param.setAttribute("info_typeDesc", paramsInfos.getInfo_typeDesc());

		} catch (Exception e) {
			e.printStackTrace();
			return "error";
		}

		return "info";
	}

	/**
	 * @Title: getInfos
	 * @Description: 查询 最新资讯 学术动态 奖励资助详情
	 * @return String
	 */
	@JSON(serialize = false)
	public String getInfos() {
		try {
			// 查询 最新资讯 学术动态 奖励资助详情
			Infos infos = indexManager.getInfos(paramsInfos);

			Param.setAttribute("infos", infos);
			Param.setAttribute("info_type", paramsInfos.getInfo_type());

		} catch (Exception e) {
			e.printStackTrace();
			return "error";
		}

		return "infoDetail";
	}

	/**
	 * @Title: listNotes
	 * @Description: 查询活动公告
	 * @return String
	 */
	public String listNotes() {
		try {
			// 查询活动公告
			Note note = new Note();
			setPagination(note);
			int[] sum = { 0 };
			List<Note> notes = indexManager.listNotes(note, sum);

			Param.setAttribute("notes", notes);
			setTotalCount(sum[0]);

		} catch (Exception e) {
			e.printStackTrace();
			return "error";
		}

		return "note";
	}

	/**
	 * @Title: getNote
	 * @Description: 查询活动公告详情
	 * @return String
	 */
	@JSON(serialize = false)
	public String getNote() {
		try {
			// 查询活动公告
			Note note = indexManager.getNote(paramsNote);
			Param.setAttribute("note", note);
		} catch (Exception e) {
			e.printStackTrace();
			return "error";
		}

		return "noteDetail";
	}

	/**
	 * @Title: listComments
	 * @Description: 查询新闻评论列表
	 * @return String
	 */
	public String listComments() {
		try {
			if ("err_random".equals(tip)) {
				tip = "验证码错误";
			} else if ("success_add".equals(tip)) {
				tip = "发布成功";
			}
			// 查询新闻评论
			Comment comment = new Comment();
			setPagination(comment);
			int[] sum = { 0 };
			List<Comment> comments = indexManager.listComments(comment, sum);

			Param.setAttribute("comments", comments);
			setTotalCount(sum[0]);
		} catch (Exception e) {
			e.printStackTrace();
			return "error";
		}

		return "comment";
	}

	/**
	 * 评论详情
	 * @Title: comment
	 * @Description: TODO
	 * @return
	 * @return String
	 */
	@JSON(serialize = false)
	public String getComment() {
		try {
			if ("success_reply".equals(tip)) {
				tip = "回复成功";
			}
			// 查询新闻评论
			Comment comment = indexManager.getComment(paramsComment);
			List<CommentReply> commentReplies = indexManager
					.listCommentReplys(paramsComment);
			String upload_pic = comment.getUpload_pic();
			String[] split = new String[0];
			if (null != upload_pic) {
				split = upload_pic.split(",");
			}
			List<String> pics = Arrays.asList(split);
			Param.setAttribute("comment", comment);
			Param.setAttribute("commentReplies", commentReplies);
			Param.setAttribute("commentPics",pics);
		} catch (Exception e) {
			e.printStackTrace();
			return "error";
		}

		return "commentDetail";
	}

	/**
	 * @Title: addComment
	 * @Description: 发布新闻评论标题
	 * @return String
	 */
	public String addComment() {
		try {
			// 验证码验证
			String random = (String) Param.getSession("random");
			if (!random.equals(paramsComment.getRandom())) {
				tip = "err_random";
				return "commentAdd";
			}
			indexManager.addComment(paramsComment);
			tip = "success_add";

		} catch (Exception e) {
			e.printStackTrace();
			return "error";
		}

		return "commentAdd";
	}
	/**
	 * @Title: addCommentReply
	 * @Description: 回复评论信息
	 * @return String
	 */
	public String addCommentReply() {
		try {
			// 回复二手信息
			indexManager.addCommentReply(paramsCommentReply);
			tip = "success_reply";

		} catch (Exception e) {
			e.printStackTrace();
			return "error";
		}

		return "commentAddReply";
	}

	/**
	 * @Title: reg
	 * @Description: 跳转注册页面
	 * @return String
	 */
	public String reg() {
		return "reg";
	}

	public Infos getParamsInfos() {
		return paramsInfos;
	}

	public void setParamsInfos(Infos paramsInfos) {
		this.paramsInfos = paramsInfos;
	}

	public Comment getParamsComment() {
		return paramsComment;
	}

	public void setParamsComment(Comment paramsComment) {
		this.paramsComment = paramsComment;
	}

	public Note getParamsNote() {
		return paramsNote;
	}

	public void setParamsNote(Note paramsNote) {
		this.paramsNote = paramsNote;
	}

	public String getTip() {
		return tip;
	}

	public void setTip(String tip) {
		this.tip = tip;
	}

	public CommentReply getParamsCommentReply() {
		return paramsCommentReply;
	}

	public void setParamsCommentReply(CommentReply paramsCommentReply) {
		this.paramsCommentReply = paramsCommentReply;
	}
}
